<?php

namespace App\Models;

use App\Common\statement\CommonStatement;
use App\Models\base\traits\HasSuperior;
use App\Models\base\traits\HasThird;
use  App\Models\base\traits\ImgTrait;
use App\Models\config\Config;
use App\Models\posts\UserFollow;
use App\Models\user\Address;
use App\Models\user\Depart;
use App\Models\user\observe\UserObserve;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;



/**
 * App\Models\User
 *
 * @property int $id
 * @property string $name
 * @property string $uuid UUID可做邀请码
 * @property string|null $password
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $header_img 用户头像
 * @property string|null $phone 手机号码
 * @property int $sex 1=>男  2=>女
 * @property float $money 用户余额
 * @property int $status 状态  1=>启用  0=>禁用  -1=>删除
 * @property int $superior_id 上级id
 * @property int $superior_uuid 上级uuid
 * @property int $level 用户等级 1 => '普通用户',2 => '小店用户',3 => '企业用户',
 * @property int $cate 用户身份类别   1 => '普通用户' , 2 => '导购' , 3 => '业务员'
 * @property int $age 年龄
 * @property string|null $birthday
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\user\Address[] $addresses
 * @property-read int|null $addresses_count
 * @property-read mixed $cate_text
 * @property-read mixed $icon
 * @property mixed $icons
 * @property mixed $img
 * @property mixed $imgs
 * @property-read mixed $level_text
 * @property mixed $parameter
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @property-read \App\Models\User $superior
 * @property-read \App\Models\user\ThirdLogin $third
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAge($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereBirthday($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereHeaderImg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereSuperiorId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereSuperiorUuid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUuid($value)
 * @mixin \Eloquent
 * @property int $is_yys 是否为育婴师
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereIsYys($value)
 * @property string $lng 用户所在经度
 * @property string $lat 用户所在纬度
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLng($value)
 * @property-read \App\Models\posts\UserFollow $follow
 * @property string|null $brief 个人简介
 * @property string|null $school 学校
 * @property string|null $bg 背景图
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereBg($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereBrief($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereSchool($value)
 * @property float $sum
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereSum($value)
 */
class User  extends  Authenticatable implements JWTSubject
{


    use ImgTrait;
    use Notifiable;
    use HasSuperior;
    use HasThird;

    public function depart()
    {
        return $this->hasOne(Depart::class,'id','depart_id');
    }

    public function getLevelTextAttribute()
    {
        return CommonStatement::level_text[$this->level];
    }

    public function getCateTextAttribute()
    {
        return CommonStatement::cate_text[$this->cate];
    }

    public function getBriefAttribute($value)
    {
        return empty($value) ? Config::getConfig('user_default_brief') : $value;
    }


    public function getSchoolAttribute($value)
    {
        return empty($value) ? Config::getConfig('user_default_school') : $value;
    }

    public function getBirthdayAttribute($value)
    {
        return empty($value) ? Config::getConfig('user_default_birthday') : $value;
    }

    public function setDepartIdAttribute($value)
    {
        return $this->attributes['depart_id'] = implode(',',$value);
    }


    public static function boot()
    {
        parent::boot();
        static::observe(new UserObserve());

    }


    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = strlen($value) == 60 ? $value : bcrypt($value);
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'nickname',
        'phone',
        'password',
        'header_img',
        'money',
        'sex',
        'age',
        'superior_id',
        'level',
        'depart_id',
        'branch_id',
        'cert_number',
        'duty',
        'run_days',
        'best_count',
        'best_dir_count'
    ];


    public function addresses()
    {
        return $this->hasMany(Address::class)->orderByDesc('is_default');
    }

    public function follow()
    {
        return $this->belongsTo(UserFollow::class,'follow_id','user_id')
            ->where('status',1);
    }


    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function getSuperiorId($depart_id)
    {
        return self::where('depart_id',$depart_id)->where('level',2)->value('id');
    }
}
